package org.example.springboot;

import jakarta.annotation.Resource;
import org.example.springboot.entities.Users;
import org.example.springboot.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@SpringBootTest
class ApplicationTests {
  @Resource
  UserService userService;

  @Test
  void contextLoads() throws NoSuchAlgorithmException {
    Users users = new Users();
    users.setUsername("wujingkai");
    users.setEmail("cswujingkai@163.com");
    // generate hash password
    MessageDigest instance = MessageDigest.getInstance("SHA-256");
    StringBuilder result = new StringBuilder();
    for (byte b : instance.digest("wujingkai".getBytes(StandardCharsets.UTF_8))) {
      result.append(String.format("%02x", b));
    }
    users.setPasswordHash(result.toString());
    users.setRole("user");
    users.setAvatar("https://assets.leetcode.cn/aliyun-lc-upload/users/wujingkai/avatar_1750558739.png");
    users.setStatus("active");
    users.setCreatedAt(new java.sql.Timestamp(System.currentTimeMillis()));
    users.setUpdatedAt(new java.sql.Timestamp(System.currentTimeMillis()));

    userService.save(users);
  }

}
